package com.dmg.sp.controller;


import com.dmg.sp.entity.Menu;
import com.dmg.sp.entity.MyUserDetails;
import com.dmg.sp.entity.Role;
import com.dmg.sp.service.RoleService;
import com.dmg.sp.utils.Result;
import com.dmg.sp.vo.IdVo;
import com.dmg.sp.vo.RoleAuthVo;
import com.dmg.sp.vo.UserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 获取所有角色
     * @return
     */
    @PostMapping("/getRoleList")
    public Result getRoleList(){
        List<Role> list=roleService.getRoleList();
        return Result.success(list);
    }

    /**
     * 添加角色
     * @return
     */
    @PostMapping("/addRole")
    public Result addRole(@RequestBody Role role){
        roleService.addRole(role);
        return Result.success();
    }

    /**
     * 修改角色
     * @return
     */
    @PostMapping("/editRole")
    public Result editRole(@RequestBody Role role){
        roleService.editRole(role);
        return Result.success();
    }

    /**
     * 删除角色
     * @return
     */
    @PostMapping("/deleteRole")
    public Result deleteRole(@RequestBody Role role){
        roleService.deleteRole(role.getId());
        return Result.success();
    }

    /**
     * 查看角色详情
     * @return
     */
    @PostMapping("/getRoleById")
    public Result getRoleById(@RequestBody Role role){
        return Result.success(roleService.getRoleById(role.getId()));
    }

    /**
     * 查看用户对应的角色
     * @return
     */
    @PostMapping("/getUserRole")
    public Result getUserRole(@RequestBody IdVo vo){
        return Result.success(roleService.getUserRole(vo.getUserId()));
    }

    /**
     * 为角色分配权限
     * @return
     */
    @PostMapping("/setRoleAuth")
    public Result setRoleAuth(@RequestBody RoleAuthVo vo){
        roleService.setRoleAuth(vo);
        return Result.success();
    }

}
